package com.qf.flash.ui.chat;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.http.response.GameDetailResponse;
import com.qf.flash.http.response.SysMsgResponse;
import com.qf.flash.mvp.contract.GameDetailContract;
import com.qf.flash.mvp.contract.SysMsgContract;
import com.qf.flash.mvp.presenter.GameDetailPresenter;
import com.qf.flash.mvp.presenter.SysMsgPresenter;
import com.qf.flash.ui.home.ChoseBusinessActivity;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 系统通知
 */
public class SystemNotificationActivity extends BaseActivity implements  SysMsgContract.View, GameDetailContract.View {

    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;

    private RVAdapter<SysMsgResponse> mAdapter;
    private SysMsgPresenter mSysMsgPresenter;
    private GameDetailPresenter gameDetailPresenter;
    private int page = 1;
    private boolean isRefresh;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, SystemNotificationActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sys_msg;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("系统通知");
        mAdapter = new RVAdapter<>(new SystemNotificationItem(this));
        list.setLayoutManager(new LinearLayoutManager(this));
        list.setAdapter(mAdapter);
        initRefresh();
        mAdapter.setOnItemChildClickListener(new RVAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                SysMsgResponse data = mAdapter.getData(position);
                String type = data.getType();
                if ("3".equals(type)) {
                    gameDetailPresenter.getGameDetail(data.getRelateId());

                }
            }
        });
    }

    private void initRefresh() {
        mRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;
                page++;
                mSysMsgPresenter.getSysMsg(page, 30);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                mSysMsgPresenter.getSysMsg(page, 30);
            }
        });
        mRefresh.autoRefresh();
    }

    @Override
    protected void initInject() {
        super.initInject();
        mSysMsgPresenter = new SysMsgPresenter();
        gameDetailPresenter = new GameDetailPresenter();
        initPresenters(mSysMsgPresenter, gameDetailPresenter);
    }


    @Override
    public void setSysMsgData(List<SysMsgResponse> responses, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore( page < totalPage);
        if (isRefresh) {
            mAdapter.setDatas(responses);
        } else {
            mAdapter.addData(responses);
        }
    }

    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }


    @OnClick(R.id.tv_top_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setGDetail(GameDetailResponse response) {
        ChoseBusinessActivity.start(this, response.getPlayId(), response.getPlayType(),
                response.getId(), response.getEmchatId(), false);
    }
}
